package com.jingbit.ai.imager;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;

import androidx.core.content.FileProvider;

import com.jingbit.ai.helper.DeviceHelper;
import com.jingbit.ai.helper.FileHelper;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;

public class ImageHelper {
	private static final String TAG="ImageHelper";
	public static final String IMAGE_CONTENT_URI = "content://";// asset目录下的资源
	public static final String IMAGE_HTTPS_URI = "https://";// 远程图片
	public static final String IMAGE_HTTP_URI = "http://";// 远程图片
	public static final String IMAGE_ASSET_URI = "asset://";// asset目录下的资源
	public static final String IMAGE_FILE_URI = "file://";// 本地文件
	public static final String IMAGE_RES_URI = "res://";// res目录下的资源
	
	public static void initImgManager(Context context){
//		ImgloadHelper.initImgLoader(context);
//		PicassoHelper.initPicasso(context);
	}
	
	// 从资源中获取Bitmap
	public static Bitmap getBitmap(Context context, int rid) {
		Resources res = context.getResources();
		return BitmapFactory.decodeResource(res, rid);
	}

	/**
	 * 根据原图和变长绘制圆形图片
	 * 
	 * @param source
	 * @param min
	 * @return
	 */
	public static Bitmap getRoundBitmap(Bitmap source, int min) {
		final Paint paint = new Paint();
		paint.setAntiAlias(true);
		Bitmap target = Bitmap.createBitmap(min, min, Config.ARGB_8888);
		/**
		 * 产生一个同样大小的画布
		 */
		Canvas canvas = new Canvas(target);
		/**
		 * 首先绘制圆形
		 */
		canvas.drawCircle(min / 2, min / 2, min / 2, paint);
		/**
		 * 使用SRC_IN，参考上面的说明
		 */
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		/**
		 * 绘制图片
		 */
		canvas.drawBitmap(source, 0, 0, paint);
		return target;
	}

	/**
	 * 获得圆角图片
	 * 
	 * @param bitmap
	 * @param roundPx
	 * @return
	 */
	public static Bitmap getRoundCornerBitmap(Bitmap bitmap, float roundPx) {
		int w = bitmap.getWidth();
		int h = bitmap.getHeight();
		Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, w, h);
		final RectF rectF = new RectF(rect);
		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		return output;
	}

	/**
	 * 获得带倒影的图片
	 * 
	 * @param bitmap
	 * @return
	 */
	public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
		final int reflectionGap = 4;
		int w = bitmap.getWidth();
		int h = bitmap.getHeight();

		Matrix matrix = new Matrix();
		matrix.preScale(1, -1);

		Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w, h / 2, matrix, false);

		Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2), Config.ARGB_8888);

		Canvas canvas = new Canvas(bitmapWithReflection);
		canvas.drawBitmap(bitmap, 0, 0, null);
		Paint deafalutPaint = new Paint();
		canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint);

		canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, null);

		Paint paint = new Paint();
		LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
				bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
		paint.setShader(shader);
		// Set the Transfer mode to be porter duff and destination in
		paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
		// Draw a rectangle using the paint with our linear gradient
		canvas.drawRect(0, h, w, bitmapWithReflection.getHeight() + reflectionGap, paint);

		return bitmapWithReflection;
	}

	/**
	 * 缩放
	 * 
	 * @param drawable
	 * @param w
	 * @param h
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static Drawable zoomDrawable(Drawable drawable, int w, int h) {
		int width = drawable.getIntrinsicWidth();
		int height = drawable.getIntrinsicHeight();
		// drawable转换成bitmap
		Bitmap oldbmp = drawableToBitmap(drawable);
		// 创建操作图片用的Matrix对象
		Matrix matrix = new Matrix();
		// 计算缩放比例
		float sx = ((float) w / width);
		float sy = ((float) h / height);
		// 设置缩放比例
		matrix.postScale(sx, sy);
		// 建立新的bitmap，其内容是对原bitmap的缩放后的图
		Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true);
		return new BitmapDrawable(newbmp);
	}

	/**
	 * 将Drawable转化为Bitmap
	 * 
	 * @param drawable
	 * @return
	 */
	public static Bitmap drawableToBitmap(Drawable drawable) {
		// 取 drawable 的长宽
		int w = drawable.getIntrinsicWidth();
		int h = drawable.getIntrinsicHeight();

		// 取 drawable 的颜色格式
		Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Config.ARGB_8888 : Config.RGB_565;
		// 建立对应 bitmap
		Bitmap bitmap = Bitmap.createBitmap(w, h, config);
		// 建立对应 bitmap 的画布
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, w, h);
		// 把 drawable 内容画到画布中
		drawable.draw(canvas);
		return bitmap;
	}

	/**
	 * Bitmap缩放,固定尺寸
	 * 
	 * @param bitmap
	 * @return
	 */
	@SuppressLint("NewApi")
	public static Bitmap zoomBitmap(Bitmap bitmap) {
		int scalew = 0, scaleh = 0;
		float width = bitmap.getWidth();
		float height = bitmap.getHeight();
		float defaultwh = DeviceHelper.getFitWidth(150);
		if (width < defaultwh && height < defaultwh) {
			return bitmap;
		}
		if (width > defaultwh || height > defaultwh) {
			int rate1 = (int) (width / defaultwh);
			int rate2 = (int) (height / defaultwh);
			int rate = rate1 > rate2 ? rate1 : rate2;
			scalew = (int) (width / rate);
			scaleh = (int) (height / rate);
		}
		if (scalew <= 0 || scalew > DeviceHelper.getFitWidth(150)) {
			scalew = DeviceHelper.getFitWidth(150);
		}
		if (scaleh <= 0 || scaleh > DeviceHelper.getFitHeight(150)) {
			scaleh = DeviceHelper.getFitHeight(150);
		}
		Bitmap newbmp = Bitmap.createScaledBitmap(bitmap, scalew, scaleh, false);
		// 小于4.1版本
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
			bitmap.recycle();
		}
		return newbmp;
	}

	// 将byte[]转换成InputStream
	public static InputStream byte2InputStream(byte[] b) {
		ByteArrayInputStream bais = new ByteArrayInputStream(b);
		return bais;
	}

	// 将InputStream转换成byte[]
	public static byte[] inputStream2Bytes(InputStream is) {
		String str = "";
		byte[] readByte = new byte[1024];
		int readCount = -1;
		try {
			while ((readCount = is.read(readByte, 0, 1024)) != -1) {
				str += new String(readByte, 0, readCount).trim();
			}
			return str.getBytes();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// 将Bitmap转换成InputStream
	public static InputStream bitmap2InputStream(Bitmap bm) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
		InputStream is = new ByteArrayInputStream(baos.toByteArray());
		return is;
	}

	// 将Bitmap转换成InputStream
	public static InputStream bitmap2InputStream(Bitmap bm, int quality) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.PNG, quality, baos);
		InputStream is = new ByteArrayInputStream(baos.toByteArray());
		return is;
	}

	// 将InputStream转换成Bitmap
	public static Bitmap inputStream2Bitmap(InputStream is) {
		return BitmapFactory.decodeStream(is);
	}

	// Drawable转换成InputStream
	public static InputStream drawable2InputStream(Drawable d) {
		Bitmap bitmap = drawable2Bitmap(d);
		return bitmap2InputStream(bitmap);
	}

	// InputStream转换成Drawable
	public static Drawable inputStream2Drawable(InputStream is) {
		Bitmap bitmap = inputStream2Bitmap(is);
		return bitmap2Drawable(bitmap);
	}

	// Drawable转换成byte[]
	public static byte[] drawable2Bytes(Drawable d) {
		Bitmap bitmap = drawable2Bitmap(d);
		return bitmap2Bytes(bitmap);
	}

	// byte[]转换成Drawable
	public static Drawable bytes2Drawable(byte[] b) {
		Bitmap bitmap = bytes2Bitmap(b);
		return bitmap2Drawable(bitmap);
	}

	// Bitmap转换成byte[]
	public static byte[] bitmap2Bytes(Bitmap bm) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
		return baos.toByteArray();
	}

	// byte[]转换成Bitmap
	public static Bitmap bytes2Bitmap(byte[] b) {
		if (b.length != 0) {
			return BitmapFactory.decodeByteArray(b, 0, b.length);
		}
		return null;
	}

	// Drawable转换成Bitmap
	public static Bitmap drawable2Bitmap(Drawable drawable) {
		Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
				drawable.getOpacity() != PixelFormat.OPAQUE ? Config.ARGB_8888 : Config.RGB_565);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
		drawable.draw(canvas);
		return bitmap;
	}

	// Bitmap转换成Drawable
	@SuppressWarnings("deprecation")
	public static Drawable bitmap2Drawable(Bitmap bitmap) {
		BitmapDrawable bd = new BitmapDrawable(bitmap);
		Drawable d = (Drawable) bd;
		return d;
	}

	/**
	 * bitmap→file
	 * 
	 * @param bitmap
	 * @return
	 */
	public static String bitmapToFile(Bitmap bitmap, String filePath) {
		File newFile = new File(filePath);
		if (!newFile.exists()) {
			try {
				newFile.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			}
		}
		FileOutputStream fStream = null;
		try {
			fStream = new FileOutputStream(newFile);
			bitmap.compress(Bitmap.CompressFormat.PNG, 100, fStream);
			fStream.flush();
		} catch (IOException e) {
			e.printStackTrace();
			Log.d(TAG, "bitmapToFile error", e);
			newFile.delete();
			newFile = null;
			return null;
		} finally {
			try {
				bitmap.recycle();
				if (fStream != null) {
					fStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			}
		}
		return newFile.getAbsolutePath();
	}

	/**
	 * 根据路径获取图片并压缩
	 * 
	 * @param imgPath
	 * @param compress
	 * @return
	 * @throws Exception
	 */
	public static Bitmap compressBitmap(String imgPath, int compress) throws Exception {
		BitmapFactory.Options newOpts = new BitmapFactory.Options();
		// 开始读入图片，此时把options.inJustDecodeBounds 设回true了
		newOpts.inJustDecodeBounds = true;
		Bitmap bitmap = BitmapFactory.decodeFile(imgPath, newOpts);// 此时返回bm为空
		newOpts.inJustDecodeBounds = false;

		int w = newOpts.outWidth;
		int h = newOpts.outHeight;
		// 现在主流手机比较多是800*480分辨率，所以高和宽我们设置为
		float currH = DeviceHelper.CURR_HEIGHT;// 当前设备高度
		float currW = DeviceHelper.CURR_WIDTH;// 当前设备宽度
		// 缩放比。由于是固定比例缩放，只用高或者宽其中一个数据进行计算即可
		int be = 1;// be=1表示不缩放
		if (w > h && w > currW) {// 如果宽度大的话根据宽度固定大小缩放
			be = (int) (newOpts.outWidth / currW);
		} else if (w < h && h > currH) {// 如果高度高的话根据宽度固定大小缩放
			be = (int) (newOpts.outHeight / currH);
		}
		if (be <= 0)
			be = 1;
		newOpts.inSampleSize = be;// 设置缩放比例
		newOpts.inPreferredConfig = Config.RGB_565;
		// 重新读入图片，注意此时已经把options.inJustDecodeBounds 设回false了
		bitmap = BitmapFactory.decodeFile(imgPath, newOpts);
		return compressBitmap(bitmap, compress);// 压缩好比例大小后再进行质量压缩
	}

	/**
	 * 质量压缩图片
	 * 
	 * @param bitmap
	 * @param compress
	 * @return
	 */
	public static Bitmap compressBitmap(Bitmap bitmap, int compress) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.PNG, compress, baos);// 质量压缩方法，这里100表示不压缩，把压缩后的数据存放到baos中
		if (compress == 0) {// 默认压缩
			int options = 100;
			while (baos.toByteArray().length / 1024 > 100) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
				baos.reset();// 重置baos即清空baos
				bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%，把压缩后的数据存放到baos中
				options -= compress;// 每次都减少10
			}
		}
		ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
		Bitmap newBitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片
		return newBitmap;
	}

	/**
	 * 自定义缩放图片
	 * 
	 * @param bitmap
	 * @param newWidth
	 * @param newHeight
	 * @return
	 */
	@SuppressLint("NewApi")
	public static Bitmap zoomBitmap(Bitmap bitmap, float newWidth, float newHeight) {
		// 获得图片的宽高
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		// // 计算缩放比例
		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;
		if (scaleHeight > scaleWidth) {
			scaleHeight = scaleWidth;
		} else {
			scaleWidth = scaleHeight;
		}
		// 取得想要缩放的matrix参数
		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);
		// 得到新的图片
		Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
		// 小于4.1版本
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
			bitmap.recycle();
		}
		return newbmp;
	}

	public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
		int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);

		int roundedSize;
		if (initialSize <= 8) {
			roundedSize = 1;
			while (roundedSize < initialSize) {
				roundedSize <<= 1;
			}
		} else {
			roundedSize = (initialSize + 7) / 8 * 8;
		}

		return roundedSize;
	}

	private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
		double w = options.outWidth;
		double h = options.outHeight;

		int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
		int upperBound = (minSideLength == -1) ? 128
				: (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));

		if (upperBound < lowerBound) {
			// return the larger one when there is no overlapping zone.
			return lowerBound;
		}

		if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
			return 1;
		} else if (minSideLength == -1) {
			return lowerBound;
		} else {
			return upperBound;
		}
	}

	/**
	 * 计算标准缩放倍数
	 * 
	 * @param options
	 * @return
	 */
	public static int calculateSampleSize(BitmapFactory.Options options) {
		// Raw height and width of image
		int inSampleSize = 1;
		final int width = options.outWidth;
		final int height = options.outHeight;
		if (height > DeviceHelper.CURR_HEIGHT || width > DeviceHelper.CURR_WIDTH) {
			// Calculate ratios of height and width to requested height and
			// width
			final int heightRatio = Math.round((float) height / (float) DeviceHelper.CURR_HEIGHT);
			final int widthRatio = Math.round((float) width / (float) DeviceHelper.CURR_WIDTH);
			inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
		}
		return inSampleSize;
	}

	/**
	 * 旋转图片
	 */
	public static Bitmap rotateBitmap(Bitmap bitmap, int rotate) {
		if (bitmap == null)
			return null;

		int w = bitmap.getWidth();
		int h = bitmap.getHeight();

		Matrix mtx = new Matrix();
		mtx.postRotate(rotate);
		return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
	}

	/**
	 * 
	 * @paraminStream
	 * @return
	 * @throws Exception
	 */
	public static byte[] streamToBytes(InputStream iStream) throws Exception {
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = iStream.read(buffer)) != -1) {
			outStream.write(buffer, 0, len);
		}
		outStream.close();
		iStream.close();
		return outStream.toByteArray();
	}

	/**
	 * 根据路径加载bitmap
	 * 
	 * @param filePath
	 *            路径
	 * @param w
	 *            款
	 * @param h
	 *            长
	 * @return
	 */
	public static Bitmap fileToBitmap(String filePath, int w, int h) {
		try {
			BitmapFactory.Options opts = new BitmapFactory.Options();
			// 设置为ture只获取图片大小
			opts.inJustDecodeBounds = true;
			opts.inPreferredConfig = Config.ARGB_8888;
			// 返回为空
			BitmapFactory.decodeFile(filePath, opts);
			int width = opts.outWidth;
			int height = opts.outHeight;
			if (w == 0) {
				w = DeviceHelper.CURR_WIDTH;
				h = DeviceHelper.CURR_HEIGHT;
			}
			float scaleWidth = 0.f, scaleHeight = 0.f;
			if (width > w || height > h) {
				// 缩放
				scaleWidth = ((float) width) / w;
				scaleHeight = ((float) height) / h;
			}
			opts.inJustDecodeBounds = false;
			float scale = Math.max(scaleWidth, scaleHeight);
			opts.inSampleSize = (int) scale;
			WeakReference<Bitmap> weak = new WeakReference<Bitmap>(BitmapFactory.decodeFile(filePath, opts));
			Bitmap bMapRotate = Bitmap.createBitmap(weak.get(), 0, 0, weak.get().getWidth(), weak.get().getHeight(),
					null, true);
			return bMapRotate;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 拍照方法
	 *
	 * @param activity
	 * @param requestCodeCamera
	 */
	public static File takePhoto(Activity activity, int requestCodeCamera) {
		// 跳转到系统的拍照界面
		Intent intentToTakePhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		// 指定照片存储位置为sd卡本目录下
		// 这里设置为固定名字 这样就只会只有一张temp图 如果要所有中间图片都保存可以通过时间或者加其他东西设置图片的名称
		// File.separator为系统自带的分隔符 是一个固定的常量

		// 获取图片所在位置的Uri路径    *****这里为什么这么做参考问题2*****
		/*imageUri = Uri.fromFile(new File(mTempPhotoPath));*/
		File takePhotoPath = FileHelper.getTempFilePath(activity, Environment.DIRECTORY_PICTURES, ".jpg");
		Uri imageUri = FileProvider.getUriForFile(activity, activity.getPackageName() + ".fileprovider", takePhotoPath);
		//下面这句指定调用相机拍照后的照片存储的路径
		intentToTakePhoto.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
		activity.startActivityForResult(intentToTakePhoto, requestCodeCamera);
		return takePhotoPath;
	}

	/**
	 * 选择视频
	 *
	 * @param activity
	 * @param code
	 */
	public static void choiceVideo(Activity activity, int code) {
		// 拍照后照片的存放目录，改成你自己拍照后要存放照片的目录。如果不传递该参数的话就没有拍照功能
		Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
		i.setType("video/*"); //选择视频 （mp4 3gp 是android支持的视频格式）
		activity.startActivityForResult(i, code);
	}

	/**
	 * 裁剪图片
	 *
	 * @param activity
	 * @param orgUri
	 * @param desUri
	 * @param aspectX
	 * @param aspectY
	 * @param width
	 * @param height
	 * @param requestCode
	 */
	public static void doCropImage(Activity activity, Uri orgUri, Uri desUri, int aspectX,
								   int aspectY, int width, int height, int requestCode) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
			intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
		}

		intent.setDataAndType(orgUri, "image/*");
		intent.putExtra("crop", "true");
		intent.putExtra("aspectX", aspectX);
		intent.putExtra("aspectY", aspectY);
		intent.putExtra("outputX", width);
		intent.putExtra("outputY", height);
		intent.putExtra("scale", true);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, desUri);
		intent.putExtra("return-data", false);
		intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
		intent.putExtra("noFaceDetection", true);
		activity.startActivityForResult(intent, requestCode);
	}

	/**
	 * open picture
	 *
	 * @param activity
	 * @param requestCode
	 */
	public static void choosePhoto(Activity activity, int requestCode) {
		Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
		photoPickerIntent.setType("image/*");
		activity.startActivityForResult(photoPickerIntent, requestCode);
	}


	private void chooseFile(Activity activity, int code) {
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.setType("file/*");
		intent.addCategory(Intent.CATEGORY_OPENABLE);
		activity.startActivityForResult(intent, code);
	}
}
